package com.originskyseed.platform.cms.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;

import com.originskyseed.platform.cms.mapper.ComplainTypeMapper;
import com.originskyseed.platform.cms.model.ComplainType;
import com.originskyseed.platform.cms.service.IComplainTypeService;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.util.bean.PrimaryKeyGeneratorUtil;

/**
 * 投诉类型表Service接口实现类（OSI_COMPLAIN_TYPE）
 *
 * @author WuZhuoXi
 */
@DubboService
@Component("complainTypeService")
public class ComplainTypeServiceImpl implements IComplainTypeService {

	@Resource
	private ComplainTypeMapper complainTypeMapper;

	/**
	 * 删除记录
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKey(String id) throws Exception {
		return this.complainTypeMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 批量删除记录
	 *
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKeys(List<String> ids) throws Exception {
		return this.complainTypeMapper.deleteByPrimaryKeys(ids);
	}

	/**
	 * 插入记录
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insert(ComplainType record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.complainTypeMapper.insert(record);
	}

	/**
	 * 插入记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertSelective(ComplainType record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.complainTypeMapper.insertSelective(record);
	}

	/**
	 * 更新记录
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKey(ComplainType record) throws Exception {
		record.setUpdateTime(new Date());
		return this.complainTypeMapper.updateByPrimaryKey(record);
	}

	/**
	 * 更新记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKeySelective(ComplainType record) throws Exception {
		record.setUpdateTime(new Date());
		return this.complainTypeMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 查询记录
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public ComplainType selectByPrimaryKey(String id) throws Exception {
		return this.complainTypeMapper.selectByPrimaryKey(id);
	}

	/**
	 * 查询列表
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<ComplainType> selectList(ComplainType record) throws Exception {
		return this.complainTypeMapper.selectList(record);
	}

	/**
	 * 分页查询列表
	 *
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<ComplainType> selectListByPage(Map<String, Object> params, BasePage<ComplainType> page) throws Exception {
		params.put("page", page);
		List<ComplainType> list = this.complainTypeMapper.selectListByPage(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 查询最大优先级
	 *
	 * @return
	 * @throws Exception
	 */
	@Override
	public Integer selectPrority() throws Exception {
		return this.complainTypeMapper.selectPrority();
	}

	/**
	 * 更新置顶记录
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateIsTop(ComplainType record) throws Exception {
		record.setUpdateTime(new Date());
		this.complainTypeMapper.updateIsTop(record);
		return this.complainTypeMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 更新排序信息
	 *
	 * @param downComplainType
	 * @param upComplainType
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updatePrority(ComplainType downComplainType, ComplainType upComplainType) throws Exception {
		downComplainType.setUpdateTime(new Date());
		upComplainType.setUpdateTime(new Date());
		this.complainTypeMapper.updateByPrimaryKeySelective(downComplainType);
		return this.complainTypeMapper.updateByPrimaryKeySelective(upComplainType);
	}

	/**
	 * 根据优先级查询投诉类型
	 *
	 * @return
	 * @throws Exception
	 */
	@Override
	public ComplainType selectComplainTypeByPrority(Integer prority) throws Exception {
		return this.complainTypeMapper.selectComplainTypeByPrority(prority);
	}

	/**
	 * 查询记录
	 *
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public ComplainType selectByPrioritySort(Map<String, Object> params) throws Exception {
		return this.complainTypeMapper.selectByPrioritySort(params);
	}
}
